import { delay, http, HttpResponse } from '@lingshu/vite-plugin-msw/msw';

function uuid() {
  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
    const r = Math.random() * 16 | 0;
    const v = c === 'x' ? r : (r & 0x3 | 0x8);
    return v.toString(16);
  });
}

const users = [
  { name: 'zhangsan', age: 18, id: uuid() },
  { name: 'lisi', age: 19, id: uuid() },
];

const handlers = [
  http.get('/api/users', () => {
    return HttpResponse.json({
      code: '0',
      message: 'success',
      data: { list: users },
    });
  }),
  http.post('/api/info', async ({ request }) => {
    const data: any = await request.json();
    let id = '';
    if (data instanceof Object) {
      id = data.id;
    }
    await delay(1000);
    if (id) {
      const index = users.findIndex(user => user.id === id);
      if (index !== -1) {
        return HttpResponse.json({
          name: users[index].name,
          age: users[index].age,
          id,
        });
      } else {
        return HttpResponse.json(
          { code: 40002, message: 'id不存在' },
          { status: 500 },
        );
      }
    } else {
      return HttpResponse.json(
        { code: 40001, message: 'id不能为空' },
        { status: 500 },
      );
    }
  }),
  http.post('/api/users', async ({ request }) => {
    await delay(1000);
    const data: any = await request.json();
    if (data instanceof Object && data.name && data.age) {
      const user = { name: data.name, age: data.age, id: uuid() };
      users.push(user);
      return HttpResponse.json({ code: '0', message: '添加成功', data: user });
    } else {
      return HttpResponse.json(
        { code: 40003, message: '无效的用户信息' },
        { status: 500 },
      );
    }
  }),
  http.put('/api/users', async ({ request }) => {
    await delay(1000);
    const data: any = await request.json();
    if (data instanceof Object && data.id && data.name && data.age) {
      const index = users.findIndex(user => user.id === data.id);
      if (index !== -1) {
        users[index] = {
          name: data.name,
          age: data.age,
          id: data.id,
        };
        return HttpResponse.json({ code: '0', message: '修改成功' });
      } else {
        return HttpResponse.json(
          { code: 40002, message: 'id不存在' },
          { status: 500 },
        );
      }
    } else {
      return HttpResponse.json(
        { code: 40003, message: '无效的用户信息' },
        { status: 500 },
      );
    }
  }),
  http.delete('/api/users', async ({ request, params }) => {
    await delay(1000);

    console.log('delete', request, params);
    const id: any = await request.text();
    if (id) {
      const index = users.findIndex(user => user.id === id);
      if (index !== -1) {
        users.splice(index, 1);
        return HttpResponse.json({ code: '0', message: '删除成功' });
      } else {
        return HttpResponse.json(
          { code: 40002, message: 'id不存在' },
          { status: 500 },
        );
      }
    } else {
      return HttpResponse.json(
        { code: 40001, message: 'id不能为空' },
        { status: 500 },
      );
    }
  }),
  http.post('/demo/test', async () => {
    return HttpResponse.json(
      { code: 0, message: '', data: { msg: 'test message' } },
    );
  }),
];

export default handlers;
